package com.kaizolo.gilabert.admin;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import org.apache.log4j.Logger;

/**
 * Servlet implementation class for Servlet: ServletImagenes
 *
 */
 public class ServletImagenes extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(ServletImagenes.class);
	private static String PATH_IMAGENES = "./workspace/gilabert/WebRoot/imagenes/max";
	private static String PATH_IMAGENES_HTML = "../imagenes/max/";

	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public ServletImagenes() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		PrintWriter out = null;
		Map respuesta = new HashMap();
		List<JSONObject> jsonObjectList = new ArrayList<JSONObject>();
		
		//Obtenemos la operaciondel post
//        String operacion = request.getParameter("operacion");
//        
//        if (operacion.equals("list")) {
//        	
//        
//        } else if (operacion.equals("new")) {
		
		try {
			File path = new File(PATH_IMAGENES);
			String[] list;
			list = path.list();
			for(int i = 0; i < list.length; i++){
				File file = new File(PATH_IMAGENES + list[i]);
				Map map = new HashMap();  
				map.put( "name", list[i] );
				map.put( "size", file.length() );
				map.put( "lastmod", file.lastModified() );
				map.put( "url", PATH_IMAGENES_HTML + list[i] );
				JSONObject jsonObject = JSONObject.fromObject( map );
				jsonObjectList.add(jsonObject);
			}
		} catch(Exception e) {
			e.printStackTrace();
	    }
		   
		JSONArray jsonArray = JSONArray.fromObject( jsonObjectList );
		respuesta.put("images", jsonArray);
		JSON jsonObject = JSONSerializer.toJSON(respuesta);
		logger.info("json: "+ jsonObject.toString() );  

		
		try{
			response.setContentType("text/html");
            //response.setCharacterEncoding("UTF-8");
            out=response.getWriter();
            out.write(jsonObject.toString());
            
        }catch (IOException e){
        	e.printStackTrace();
        }
	}

}